package mmoon.mmoonstory.commands;

import mmoon.mmoonstory.EasyCommand;
import mmoon.mmoonstory.interfaces.InitCommand;
import net.minecraft.command.CommandBase;
import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.command.WrongUsageException;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.server.MinecraftServer;

import java.util.Objects;

public class CommandSpeed extends CommandBase implements InitCommand {
    public CommandSpeed() {
        this.init();
    }

    @Override
    public String getName() {
        return "speed";
    }

    @Override
    public String getUsage(ICommandSender sender) {
        return "/speed value [<walk|fly>]";
    }

    @Override
    public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException {
        if (sender.getCommandSenderEntity() instanceof EntityPlayer) {
            EntityPlayer player = (EntityPlayer) sender.getCommandSenderEntity();
            if (args.length <= 2) {
                if (args.length == 0) {
                    EasyCommand.msg(player, "Your walk speed is " + player.capabilities.getWalkSpeed());
                    EasyCommand.msg(player, "Your fly speed is " + player.capabilities.getFlySpeed());
                } else if (args.length == 2) {
                    if (Objects.equals(args[1], "walk")) {
                        player.capabilities.setPlayerWalkSpeed(Float.parseFloat(args[0]));
                    } else if (Objects.equals(args[1], "fly")) {
                        player.capabilities.setFlySpeed(Float.parseFloat(args[0]));
                    } else {
                        throw new WrongUsageException(this.getUsage(sender));
                    }
                } else {
                    throw new WrongUsageException(this.getUsage(sender));
                }
            } else {
                throw new WrongUsageException(this.getUsage(sender));
            }
        }
    }

    @Override
    public int getRequiredPermissionLevel() {
        return 3;
    }
}
